<%@page import="multiface.awwadco.jpa.Utente"%>
<%
    Utente ut = (Utente) session.getAttribute("admin");
    if (ut == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>

<%@page import="multiface.web.util.categories.AlberoCategorieProdotti"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="multiface.awwadco.jpa.Categoria"%>
<script>
    $("#select-moveto-cat").change(function () {
        $.post("right/position_cat_prod.jsp",{
            cid: $(this).val()
        },function(data){
            $("#moveto_cat_pos").fadeIn(400, function(){
                $("#moveto_cat_pos").html(data); 
            });
        });
    });
</script>
<div class="title">Commands</div>
<ul>
    <li id="add_cat"><a href="javascript:open('add_prod_cat.jsp')">New Category</a></li>
</ul>
<div style="display: none" id="title_cat" class="title">Select</div>
<ul>
    <li style="display: none" id="mod_cat"><a href="javascript:open('mod_prod_cat.jsp')">Modify Category </a></li>
    <li style="display: none" id="del_cat"><a href="javascript:del('cat')">Delete Category </a></li>
    <li style="display: none" id="moveto_cat">
        Move To:
        <select id="select-moveto-cat">
            <option value="-2" checked >        </option>
            <option value="-1">Category</option>
            <%
                AlberoCategorieProdotti tree = new AlberoCategorieProdotti();
                tree.caricaDaDB();

                List<Categoria> lcat = tree.categorieInOrdineDiProfondita();
                Iterator it = lcat.iterator();
                int sc = -1;
                int csc = 0;
                int p = 0;
                int[] arrpos = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

                while (it.hasNext()) {
                    Categoria c = (Categoria) it.next();
                    if (c.getSopracategoria() > sc) {
                        // entro in una sottocategoria
                        csc++;
                        p++;
                        sc = c.getSopracategoria();
                        //
                        //out.println("<ul id=\"sottoli_" + sc + "\" class=\"ul_close\">");
                    }
                    if (c.getSopracategoria() < sc) {
                        //esco da una sottocategoira
                        int h = tree.Altezza(c);
                        //Sy_stem.out.println("[h=" + h + "]");
                        int m = (p - h) + 1;
                        for (int i = 0; i < m; i++) {
                            arrpos[p] = 0;
                            p--;
                            sc = c.getSopracategoria();
                            //out.println("</ul>");
                        }
                    }

                    arrpos[p] = arrpos[p] + 1;
                    String pre = "";
                    for (int i = 0; i < p + 1; i++) {
                        pre += (arrpos[i] + "");
                        if (i < p) {
                            pre += ".";
                        }
                    }
                    out.print("<option value=\"" + c.getIdcategoria() + "\">" + pre + " - " + c.getNome() + "</option>");
                }
            %>
        </select>
    </li>
    <div class="li-move-to" id="moveto_cat_pos"></div>
</ul>
